<?php
class Habits extends CI_Model{
	
	function get_habit_info($habit_id){
		$today_time = strtotime(date('Y-m-d',time()));
		$sql = "SELECT task.*, type.name AS type_name, type.image AS type_img, type.pid, type.share_title, type.share_des, record.id AS record_id, record.stars AS reward_stars, uc.head_img_url, uc.nick, uc.gender FROM {$this->db->dbprefix}tasks task ";
		$sql .= "LEFT JOIN {$this->db->dbprefix}task_types AS type ON(type.id = task.task_type_id) ";
		$sql .= "LEFT JOIN {$this->db->dbprefix}task_records AS record ON(record.task_id = task.id and record.time >= $today_time) ";
		$sql .= "LEFT JOIN {$this->db->dbprefix}user_childrens AS uc ON(uc.id=task.child_id) ";
		$sql .= "WHERE task.id = $habit_id ";
		$result = $this->db->query($sql)->row_array();
		return $result;
	}

			//获取本周的任务记录
	function get_week_habit_list($task_id){
		$first=1;
        $sdefaultDate = date("Y-m-d");
        $w=date('w',strtotime($sdefaultDate));
        $week_start_time=strtotime(date('Y-m-d',strtotime("$sdefaultDate -".($w ? $w - $first : 6).' days')));
		$sql = "SELECT * FROM {$this->db->dbprefix}task_records WHERE task_id='{$task_id}' AND time > $week_start_time ORDER BY id DESC";
		$result = $this->db->query($sql)->result_array();
		return $result;
	}

	function get_recommend_list($habit_id){
		$sql = "SELECT hc.*, a.title, a.remark FROM {$this->db->dbprefix}habit_content AS hc ";
		$sql .= " LEFT JOIN {$this->db->dbprefix}articles AS a ON(a.id = hc.content_id) ";
		$sql .= " WHERE hc.habit_id = '{$habit_id}' AND hc.status = 1 ORDER BY sort DESC ";
		$result = $this->db->query($sql)->result_array();
		return $result;
	}

	function get_hot_habit_list(){
		$sql = "SELECT SQL_CALC_FOUND_ROWS * FROM {$this->db->dbprefix}task_types WHERE is_hot = 1 ORDER BY id DESC LIMIT 5";
		$result = $this->db->query($sql)->result_array();
		return $result;
	}
}